package com.boco.pciv.controller.sys;


import cn.hutool.core.util.StrUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.core.exception.PcivException;
import com.boco.pciv.entity.sys.RoleInfo;
import com.boco.pciv.service.sys.IResService;
import com.boco.pciv.service.sys.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 操 on 2017/01/17.
 */
@RestController
@RequestMapping(value="/sys/role")
public class RoleController extends PcivController {

    Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private IRoleService svr;
    @Autowired
    private IResService resSer;

    @GetMapping("/list")
    public PageResultBase getRoleList(String roleName, LayUIPager pageParam){
        SearchCriteria param = new SearchCriteria();
        param.setPager(pageParam);
        param.putCriteria("RoleName", roleName);
        PageResultBase result= svr.getPage(param);
        addOperationLog("查询","角色查询");
        return result;
    }
    @GetMapping("/info")
    public BizResponse getRoleInfo(String id){
        RoleInfo result= svr.getByRoleId(id);
        return BizResponseUtil.single(result);
    }

    @PostMapping("/add")
    public BizResponse addRole(RoleInfo role){
        if(role==null) {
            return BizResponseUtil.fail("新增信息不能为空");
        }
        if (StrUtil.isBlank(role.getRoleId())){
            return BizResponseUtil.fail("角色ID不能为空");
        }
        if (StrUtil.isBlank(role.getRoleName())){
            return BizResponseUtil.fail("角色名称不能为空");
        }

//        String rid = role.getRoleId();
//        RoleInfo u = svr.getByRoleId(rid);
//        if(u!= null){
//            return BizResponseUtil.fail("该角色已经存在");
//        }

        try{
            role.setCreateUser(getCurUserId());
            svr.add(role);
            addOperationLog("新增",String.format("新增角色:%s",role.getRoleId()));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }

    @PostMapping("/delete")
    public BizResponse deleteRole(String id){
        try{
            svr.deleteRole(id);
            addOperationLog("删除",String.format("删除角色:%s",id));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }
    @PostMapping("/edit")
    public BizResponse editRole(RoleInfo role){
        if(role ==null){
            return BizResponseUtil.fail("角色信息不能为空");
        }
        if (StrUtil.isBlank(role.getRoleId())){
            return BizResponseUtil.fail("角色ID不能为空");
        }
        if (StrUtil.isBlank(role.getRoleName())){
            return BizResponseUtil.fail("角色名称不能为空");
        }

        try{
            role.setUpdateUser(getCurUserId());
            svr.edit(role);
            addOperationLog("编辑",String.format("编辑角色:%s",role.getRoleId()));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }
    @GetMapping("/checkRoleIdValidate")
    public String checkRoleIdExists(String roleId){
        RoleInfo info = svr.getByRoleId(roleId);
        if(info!=null){
            return "false";
        }
        else {
            return "true";
        }
    }
}
